import pygame
from pygame.sprite import Sprite

"""飞船管理类"""


class Ship(Sprite):
    def __init__(self, ai_game):
        super().__init__()
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        # 记载飞船图像并获取其外接矩形
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        # 每艘飞船都放在屏幕底部
        self.rect.midbottom = self.screen_rect.midbottom
        # 移动标志（飞船一开始移动）
        self.moving_right = False
        self.moving_left = False
        self.settings = ai_game.settings
        self.x = float(self.rect.x)

    def bitme(self):
        self.screen.blit(self.image, self.rect)

    def update(self):
        """根据移动标志跳转飞船的位置"""
        if self.moving_right and (self.rect.right < self.screen_rect.right):
            self.x += self.settings.ship_speed
        if self.moving_left and (self.rect.left > 0):
            self.x -= self.settings.ship_speed
        self.rect.x = self.x

    def center_ship(self):
        """将⻜船放在屏幕底部的中央"""
        self.rect.midbottom = self.screen_rect.midbottom
        self.x = float(self.rect.x)
